#include <iostream>
#include <string.h>
#include <vector>
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::string;
class String 
{
public:
	String()
    :_pstr(new char[1])
    {

    }
	String(const char *pstr)
    :_pstr(new char[strlen(pstr)+1]())
    {
        strcpy(_pstr,pstr);
    }
    //拷贝构造函数
    String(const String & rhs)
    :_pstr(new char[strlen(rhs._pstr)+1])
    {
    strcpy(_pstr,rhs._pstr);
    }
    ~String(){
        delete [] _pstr;
        _pstr=nullptr;
    }
    //赋值运算符函数，传参对象
    String &operator=(const String &rhs){
        if(this!=&rhs){
            delete [] _pstr;
            _pstr=new char [strlen(rhs._pstr)+1]();
            strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }
    //赋值运算符函数,传参字符串
    String &operator=(const char *pstr){
        String temp=String(pstr);
        return *this;
    }
    //字符串的拼接，传参对象
    String &operator+=(const String &rhs){
        char *temp=new char[strlen(_pstr)+strlen(rhs._pstr)+1]();
        strcpy(temp,_pstr);
        strcat(temp,rhs._pstr);
        _pstr=temp;
        return *this;
    }//字符串拼接，传参字符串
    String &operator+=(const char *pstr){
        *this+=String(pstr);
        
        return *this;
    }
	//求下标
    char &operator[](std::size_t index){
        if(index>=strlen(_pstr)){
            static  char  ch='\0';
            return ch;
        }
       return _pstr[index];
    }
    //常量版本下标运算符
    const char &operator[](std::size_t index) const{
        if(index>=strlen(_pstr)){
            static char ch1='\0';
            return ch1;
        }
        return _pstr[index];
    }
   
    std::size_t size() const{
        return strlen(_pstr);
    }
    const char* c_str() const;
	friend bool operator==(const String &rhs ,const String &lhs);
	friend bool operator!=(const String &rhs, const String &lhs);
	
	friend bool operator<(const String &rhs, const String &lhs);
	friend bool operator>(const String &rhs, const String &lhs);
	friend bool operator<=(const String &rhs, const String &lhs);
	friend bool operator>=(const String &rhs, const String &lhs);
	
	friend std::ostream &operator<<(std::ostream &os, const String &s);
	friend std::istream &operator>>(std::istream &is, String &s);
    void display(){
        cout<<_pstr<<endl;
    }
   friend String operator+(const String &rhs,const String &lhs);
   friend String operator+(const String & rhs,const char *pstr);
   friend String operator+(const char*pstr1,const String &rhs);

private:
	char * _pstr;
};

std::ostream &operator<<(std::ostream &os,const String &s){
    os<<s._pstr;
    return os;
}
std::istream &operator>>(std::istream &is,String &s){
    delete [] s._pstr;
    vector<char> vec;
    char c;
    while((c=is.get())!='\n'){
        vec.push_back(c);
    }
    s._pstr=new char[vec.size()+1]();
    for(int i=0;i<vec.size();++i){
        s._pstr[i]=vec[i];
    }
    return is;
}
String operator+(const String &rhs, const String &lhs){
    String temp=rhs;
    temp+=lhs;
    return temp;
}
String operator+(const String &rhs, const char *pstr){
    String temp=rhs;
    temp+=pstr;
    return temp;
}
String operator+(const char *pstr1, const String &rhs){
    String temp(pstr1);
    temp+=rhs._pstr;
    return temp;
}
bool operator==(const String &rhs, const String &lhs){
    return strcmp(rhs._pstr,lhs._pstr)==0;
}               
bool operator!=(const String &rhs, const String &lhs){
    return !(rhs==lhs);
}
bool operator<(const String &rhs, const String &lhs){
    return strcmp(rhs._pstr,lhs._pstr)<0; 
}
bool operator>(const String &rhs, const String &lhs){
    return lhs<rhs;
}
bool operator<=(const String &rhs, const String &lhs){
    return (rhs<lhs)||(rhs==lhs);
}
bool operator>=(const String &rhs, const String &lhs){
    return (lhs<rhs)||(rhs==lhs);
}

void test(){
//无参构造函数
String s1;
s1.display();
//构造函数
String s2("hello");
s2.display();
//赋值运算符函数
String s3=s2;
s3.display();
//赋值运算符函数
String s4=("world");
//下标
cout<<"========下标======="<<endl;
cout<<s4[1]<<endl;
//下标
const String s7=("nihao");
cout<<s7[1]<<endl;
//字符串拼接,传参对象
cout<<"========字符串拼接========="<<endl;
String s5=s2;
s5+=s4;
s5.display();
//字符串拼接，传参字符串
String s6="hello";
s6+="world";
s6.display();
//长度
cout<<s6.size()<<endl;
}
void test1(void){
//友元函数，两个对象相加
cout<<"=========两个对象相加=========="<<endl;
String s2("hello");
String s3("world");
String s1=s2+s3;
s1.display();
String s4="hello"+s3;
s4.display();
String s5=s2+"world";
s5.display();
}
void test2(){
    cout<<"==========比较运算符========="<<endl;
    String s1("apple");
    String s2("banana");
    String s3("pear");
    String s4("apple");
    cout<<(s2==s4)<<endl;
    cout<<(s1<=s2)<<endl;
    cout<<(s1>s3)<<endl;
    cout<<(s1<=s3)<<endl;

}
void test3(){
    cout<<"============流运算符========="<<endl;
    String s1("hello");
    cout<<s1<<endl;
    cin>>s1;
    cout<<s1<<endl;
}
int main (){
    test();
    test1();
    test2();
   test3();
    return 0;
}
